package com.zlt.bkparking.controller.admin;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.zlt.bkparking.dto.*;
import com.zlt.bkparking.entity.Permission;
import com.zlt.bkparking.entity.Role;
import com.zlt.bkparking.service.AdminService;
import com.zlt.bkparking.utils.StringUtil;
import com.zlt.bkparking.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@RequestMapping("adminUser")
public class AdminUserController {

    @Autowired
    private DefaultKaptcha defaultKaptcha;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AdminService adminService;

    @GetMapping("kaptcha")
    public Result kaptcha(HttpServletResponse response){
        String text = defaultKaptcha.createText();
        log.debug("验证码为{}",text);
        String uuid = StringUtil.uuid();
        response.setHeader("token",uuid);
        //将生成的文字放入redis
        redisTemplate.opsForValue().set(uuid,text,5, TimeUnit.MINUTES);
        //创建图片
        BufferedImage image = defaultKaptcha.createImage(text);
        //将图片转换成字符串
        String s = StringUtil.changeImage2String(image);
        return Result.ok("获取成功",s);
    }

    @PostMapping("login")
    public Result login(AdminUserDTO adminUserDTO, HttpServletResponse response){

        try {
            boolean login = adminService.login(adminUserDTO);
//            userService.selectUserByUsername(userDTO.getUsername());
            response.setHeader("token","true");
            return login ? Result.ok("登录成功") : Result.error("登录失败");
        } catch (RuntimeException e){
            log.error("s");
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("menus")
    public Result menus(AdminUserDTO adminUserDTO, HttpServletResponse response){
        try {
            boolean login = adminService.login(adminUserDTO);
//            userService.selectUserByUsername(userDTO.getUsername());
            response.setHeader("token","true");

            return Result.ok("登录成功", login);
        } catch (RuntimeException e){
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("roleList")
    public Result roleList(){
        List<Role> roleList =  adminService.selectAllRoleList();
        return Result.ok("查询成功",roleList);
    }


        @PostMapping("roleListSearch")
    public Result roleListSearch(RoleDTO roleDTO){
        List<Role> roleList =  adminService.selectAllRoleListearch(roleDTO.getRName());
        return Result.ok("查询成功",roleList);
    }

    @PostMapping("save")
    public Result roleSave(RoleDTO roleDTO){
        boolean success =  adminService.saveRole(roleDTO);
        return  success ? Result.ok("保存成功") : Result.error("保存失败");
    }


    @PostMapping("deleteRole")
    public Result deleteRole(RoleDTO roleDTO){
        boolean success =  adminService.deleteRole(roleDTO);
        return  success ? Result.ok("删除成功") : Result.error("删除失败");
    }


    @PostMapping("robakRole")
    public Result robakRole(RoleDTO roleDTO){
        boolean success =  adminService.robakRole(roleDTO);
        return  success ? Result.ok("恢复成功") : Result.error("恢复失败");
    }


    @GetMapping("tree")
    public Result getTree(){
        List<PermissionList> permissionListList =  adminService.getRoles();
        return  Result.ok("查询成功",permissionListList);
    }
    @PostMapping("ownRole")
    public Result ownRole(RoleDTO roleDTO){
        List<Permission> permissionListList =  adminService.getOwnRoles(roleDTO.getRId());
        return  Result.ok("查询成功",permissionListList);
    }


    @PostMapping("saveAdmin")
    public Result saveAdminRolePer(RolePermissionsVO rolePermissionsVO){
        Boolean success =  adminService.saveAdminPer(rolePermissionsVO);
        return  success ? Result.ok("保存成功") : Result.error("保存失败");
    }


    @PostMapping("allPermissionList")
    public Result allPermissionList(){
        List<PermissionsVO> permissionsVOS = adminService.getAllPermission();
        return Result.ok("查询成功",permissionsVOS);
    }

    @PostMapping("nodes")
    public Result getNodes(){
        List<Permission> permissions = adminService.getNodes();
        return Result.ok("查询成功",permissions);
    }

    @PostMapping("getOne")
    public Result getOne( PermissionsDTO permissionsDTO){
        Permission permission = adminService.getPermissionByPid(permissionsDTO.getId());
        return Result.ok("查询成功",permission);
    }

    @PostMapping("savePermission")
    public Result savePermission( PrDTO permissionsDTO){
        Boolean success = adminService.savePermission(permissionsDTO);
        return  success ? Result.ok("保存成功") : Result.error("保存失败");
    }


    @PostMapping("yewuUserList")
    public Result yewuUserList( BusinessUserDTO businessUserDTO){
        Pager<BusinessUserVO> page = adminService.getAllUser(businessUserDTO);
        return Result.ok("查询成功",page);
    }

    @PostMapping("userSave")
    public Result userSave( BusinessUserDTO businessUserDTO){
        Boolean success = adminService.userSave(businessUserDTO);
        return  success ? Result.ok("保存成功") : Result.error("保存失败");
    }

    @PostMapping("userDeleteAdmin")
    public Result userDeleteAdmin( BusinessUserDTO businessUserDTO){
        Boolean success = adminService.userDeleteAdmin(businessUserDTO);
        return  success ? Result.ok("删除成功") : Result.error("删除失败");
    }

    @PostMapping("pwd")
    public Result pwd( BusinessUserDTO businessUserDTO){
        System.out.println(businessUserDTO);
        Boolean success = adminService.pwd(businessUserDTO);
        return  success ? Result.ok("删除成功") : Result.error("删除失败");
    }
}
